<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/16
 * Time: 15:27
 */
namespace app\common\logic;

class Region extends Base {

    private $region_model;

    public function __construct()
    {
        parent::__construct();
        $this->region_model = model("Region");
    }

    /**
 * 获取地区id对应的地区详细信息
 * @param $district_id
 * @return array
 */
    public function getDistrictInfo($district_id)
    {
        $district_info = $this->region_model->getInfo([
            'region_id'     => $district_id,
            'region_type'   => 3,//区县级
        ]);

        if (empty($district_info)) {
            return [];
        }

        //获取省市id信息
        $city_info = $this->region_model->getInfo([
            'region_id'     => $district_info['parent_id'],
            'region_type'   => 2,//市级
        ]);
        if (empty($city_info)) {
            exception("地区数据异常！");
        }

        $province_info = $this->region_model->getInfo([
            'region_id'     => $city_info['parent_id'],
            'region_type'   => 1,//省级
        ]);

        return [
            'district_info' => $district_info,
            'city_info'     => $city_info,
            'province_info' => $province_info,
        ];
    }

    /**
     * 获取地区id对应的地区详细信息
     * @param $region_id
     * @return array
     */
    public function getRegionInfo($region_id)
    {
        return $this->region_model->getInfo([
            'region_id'     => $region_id,
        ]);
    }

    /**
     * 获取指定地区的所有子级地区信息
     *
     * @author mayy
     * @date 2017-06-27
     * @param integer $parent_id 父级地区id
     * @return array
     */
    public function getSonRegionsByParentId($parent_id)
    {
        if ($parent_id <= 0) {
            return [];
        }

        $fields = 'region_id,region_name,first_char';
        $region_data = $this->region_model->getList(['parent_id' => $parent_id], 1000, false, $fields);
        return $region_data;
    }
}